package cn.com.guanying.android.ui;

import java.util.Hashtable;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import cn.com.guanying.R;
import cn.com.guanying.android.ui.dialogs.GuanyingDialog;

public class HomeGroupActivity extends ActivityGroup implements OnClickListener {

	private RadioButton mRadioButtonNowon;
	private RadioButton mRadioButtonAboutto;
	private LinearLayout mLayout_container;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_home_group);
		init();
	}

	public void init() {
		mRadioButtonNowon = (RadioButton) findViewById(R.id.radioButtonNowon);
		mRadioButtonAboutto = (RadioButton) findViewById(R.id.radioButtonAboutto);
		mLayout_container = (LinearLayout) findViewById(R.id.layout_container);
		mRadioButtonNowon.setOnClickListener(this);
		mRadioButtonAboutto.setOnClickListener(this);
		mRadioButtonNowon.setChecked(true);
		setTabvew(HomeActivity.class);
	}

	public Hashtable<String, View> mViewHashtable = new Hashtable<String, View>();

	private View currentView = null;
	
	private void setTabvew(Class<?> cla)
	{
		mLayout_container.removeAllViews();
		mLayout_container.addView(getCurrentShowView(cla));
	}

	public View getCurrentShowView(Class<?> cla) {
		/*
		 * LocalActivityManager m = getLocalActivityManager(); Intent intent = new Intent().setClass(this, cla); Window w = m.startActivity("tratat", intent); View v = w.getDecorView();
		 */
		String name = cla.getName();
		currentView = (View) mViewHashtable.get(name);
		if (currentView != null)
			return currentView;
		LocalActivityManager m = getLocalActivityManager();
		Intent intent = new Intent().setClass(this, cla);
		Window w = m.startActivity(cla.getSimpleName(), intent);
		currentView = w.getDecorView();
		mViewHashtable.put(name, currentView);
		return currentView;
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			showExitDialog();
			return true;
		}
		return false;
	}
	public void exit(){
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	public void showExitDialog(){
		final GuanyingDialog gyDlg = new GuanyingDialog(this);
		gyDlg.setButton2(R.string.quxiao, new OnClickListener() {
			public void onClick(View v) {
				gyDlg.dismiss();
			}
		});
		gyDlg.setButton1(R.string.queding, new OnClickListener() {
			public void onClick(View v) {
				exit();
				gyDlg.dismiss();
			}
		});
		gyDlg.setInfo(R.string.tuichutishi);
		gyDlg.show();
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view.equals(mRadioButtonNowon)) {
			setTabvew(HomeActivity.class);
		}else if(view.equals(mRadioButtonAboutto)){
			setTabvew(ToBePlayMovieActivity.class);
		}
	}

}
